package com.bjsxt.user.client.controller;

import com.bjsxt.pojo.User;
import com.bjsxt.rpc.BjsxtRpcFactory;
import com.bjsxt.user.service.UserService;
import org.apache.zookeeper.KeeperException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
//@Autowired增加了会报错，不明原因
    private UserService userService;

    public UserController() {
        try {
            this.userService = BjsxtRpcFactory.getServiceProxy(UserService.class);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("服务'" + UserService.class.getName() + "'不存在异常");
        }
    }

    @RequestMapping("/getUsersByName")
    @ResponseBody
    public List<User> getUsersByName(String name) {
        try {
            List<User> users = userService.getUsersByName(name);
            return users;
        } catch (RemoteException e) {
            e.printStackTrace();
            return new ArrayList<User>();
        }
    }
}
